using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Fields;
using Microsoft.SharePoint.Publishing.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.SharePoint.WebControls;

namespace GetSharePointToValidate.Adapters
{
    public class GSPTV_RichImageField : ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.Page is Microsoft.SharePoint.Publishing.PublishingLayoutPage && SPContext.Current.FormContext.FormMode.Equals(SPControlMode.Display))
            {
                RichImageField RichImage = this.Control as RichImageField;
                ImageFieldValue imageFieldValue = RichImage.ItemFieldValue as ImageFieldValue;
                StringBuilder stringBuilder = new StringBuilder();
                using (StringWriter stringWriter = new StringWriter(stringBuilder))
                {
                    using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
                    {

                        if (imageFieldValue != null)
                        {
                            Image image = new Image();
                            image.ImageUrl = imageFieldValue.ImageUrl;
                            image.AlternateText = imageFieldValue.AlternateText;
                            image.GenerateEmptyAlternateText = true;
                            if (!imageFieldValue.Width.Equals(0))
                                image.Width = imageFieldValue.Width;
                            if (!imageFieldValue.Height.Equals(0))
                                image.Height = imageFieldValue.Height;
                            if (!imageFieldValue.BorderWidth.Equals(0))
                                image.BorderWidth = imageFieldValue.BorderWidth;


                            if (string.IsNullOrEmpty(imageFieldValue.Hyperlink))
                            {
                                //For some risen a href attribute is rended :S
                                image.RenderControl(htmlTextWriter);
                            }
                            else
                            {
                                HyperLink hyperLink = new HyperLink();
                                hyperLink.NavigateUrl = imageFieldValue.Hyperlink;
                                if (imageFieldValue.OpenHyperlinkInNewWindow)
                                    hyperLink.Attributes.Add("rel", "external");

                                hyperLink.Controls.Add(image);
                                hyperLink.RenderControl(htmlTextWriter);
                            }
                            writer.Write(stringBuilder.ToString());
                        }
                    }
                }
            }
            else
                base.Render(writer);
        }
    

    }
}
